package com.loiot.baqi.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.usermodel.Row;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.loiot.baqi.constant.Const;
import com.loiot.baqi.controller.response.AjaxResponse;
import com.loiot.baqi.controller.response.Pager;
import com.loiot.baqi.pojo.TgQqGroup;
import com.loiot.baqi.pojo.ZpCompanyInfo;
import com.loiot.baqi.security.shiro.UsernameExistException;
import com.loiot.baqi.service.AccountService;
import com.loiot.baqi.service.RoleService;
import com.loiot.baqi.service.TgQqGroupService;
import com.loiot.baqi.status.AccountType;
import com.loiot.baqi.status.PauseStartType;
import com.loiot.baqi.utils.ExcelUtil;
import com.loiot.baqi.utils.UserSessionUtils;
import com.loiot.commons.utils.JsonUtil;
import com.timeloit.pojo.Account;

/**
 * 导入excel
 * 
 * @author zhengrj
 * 
 */
@Controller
@RequestMapping(value = "/exportExcel")
public class ExportExcelController {

    public static final AjaxResponse ACCOUNT_USERNAME_EXIST = new AjaxResponse(-100301, "用户名已存在");

    private Logger log = LoggerFactory.getLogger(this.getClass());
    
    @Autowired
    TgQqGroupService tgQqGroupService;

   
    /**
     * 跳转后台用户列表页
     * 
     * @return 模板位置
     * @throws Exception 
     */
    @RequestMapping(value = "/import")
    public String exportExcel(	) throws Exception {
    	ExcelUtil eu = new ExcelUtil();  
        
        //从第一行开始读取  
        eu.setStartReadPos(1);  
          
        String src_xlspath = "D:\\招聘qq群.xls";  
        //String dist_xlsPath = "D:\\1.xls";  
        List<Row> rowList;  
        
        try {
        	
            rowList = eu.readExcel(src_xlspath); 
            int initCount=0;
          //从第一行开始读取  
            for(int i=0;i<rowList.size();i++){
            		initCount=0;
 	            	Row row = rowList.get(i);
 	            	
 	            	if(row.getCell(0)==null || row.getCell(0).toString().equals("null") || String.valueOf(row.getCell(0)).length()==0){
 	            		initCount++;
 	            	}
 	            	if(row.getCell(1)==null || row.getCell(1).toString().equals("null") || String.valueOf(row.getCell(1)).length()==0){
 	            		initCount++;
 	            	}
 	            	if((row.getCell(2)==null || row.getCell(2).toString().equals("null")) || String.valueOf(row.getCell(2)).length()==0){
 	            		initCount++;
 	            	}
 	            	if(initCount>0){
 	            		continue;
 	            	}
 	            	           	
 	            	row.getCell(0).setCellType(HSSFCell.CELL_TYPE_STRING);
 	            	row.getCell(1).setCellType(HSSFCell.CELL_TYPE_STRING);
 	            	row.getCell(2).setCellType(HSSFCell.CELL_TYPE_STRING);
 	            	
 	            	String name = row.getCell(0).getStringCellValue();
 	            	String number = row.getCell(1).getStringCellValue();
 	            	String desc = row.getCell(2).getStringCellValue();
 	            	//25	9	one	java
 	            	//26	9	two	php
 	            	//27	9	three	android
 	            	if(desc!=null && desc.trim().length()>0){
 	            		initCount++;
 	            		//String sql = "insert into tg_qq_group (number,name,validate_message,category,in_person)  values ('"+number+"','"+name+"'"+",'"+desc+"',25,1)" ;  
 	 	            	//System.out.println(sql );
 	            		TgQqGroup t = new TgQqGroup();
 	            		t.setName(name);
 	            		t.setNumber(number);
 	            		t.setCategory(27l);
 	            		t.setInTime(new Date());
 	            		t.setValidateMessage(desc);
 	            		t.setInPerson(1l);
 	            		tgQqGroupService.addTgQqGroup(t);
 	            	}
             		
            
            }
            System.out.println(initCount);
            
            String af="";
            //eu.writeExcel_xls(rowList, src_xlspath, dist_xlsPath);  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    	
    	
        return "/account/account_list";
    }
    
  

}
